package core;

import database.*;
import utils.ExportExcelUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jimmyhsu on 2016/12/14.
 */
public class ExportCommentTable extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] titles = new String[]{"课程#className","课程内容#classContent"
                ,"教师水平#teacherCapability","所得收获#personalGain"};
        List<Course> courses = CourseDb.selectAllCourses();
        List<CommentBean> datas = new ArrayList<CommentBean>();
        for (Course course : courses) {
            int courseId = course.getId();
            String courseName = course.getName();
            float[] comment = CommentDb.selectAvgRatingByCourseId(courseId);
            CommentBean bean = new CommentBean(courseName, comment[0], comment[1], comment[2]);
            datas.add(bean);
        }
        try {
            ExportExcelUtil.export(resp, "学生评价表", "学生评价", titles, datas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
